<template>
  <el-menu :default-active="activeMenu" mode="horizontal" @select="handleSelect">
    <el-menu-item index="/index" @click="handleIndex">
      <svg-icon :icon-class="'dashboard'" />首页
    </el-menu-item>
    <template v-for="(item, rowLength) in topMenus">
      <el-menu-item :index="item.path" :key="item.path" style="margin-left: 15px" v-if="rowLength < visibleNumber && (!item.children || item.children.length == 0)">
        <template #title>
          <svg-icon :icon-class="item.meta.icon" style="margin-right: 7px !important" />
          <div>{{ item.meta.title }}</div>
        </template>
      </el-menu-item>
      <el-sub-menu :index="item.path" :key="item.path" style="margin-left: 15px" v-if="rowLength < visibleNumber && item.children && item.children.length > 0">
        <template #title>
          <svg-icon :icon-class="item.meta.icon" style="margin-right: 7px !important" />
          <div>{{ item.meta.title }}</div>
        </template>

        <template v-for="(children, index) in item.children">
          <el-menu-item v-if="!children.children" :key="index" :index="children.path">
            <template #title>
              <svg-icon :icon-class="children.meta.icon" style="margin-right: 2px !important" />
              {{ children.meta.title }}
            </template>
          </el-menu-item>
        </template>

        <template v-for="(children, index) in item.children">
          <el-sub-menu :key="index" :index="children.path" v-if="children.children">
            <template #title>
              <svg-icon :icon-class="children.meta.icon" style="margin-right: 2px !important; color: #000" />
              <div style="color: #000">{{ children.meta.title }}</div>
            </template>

            <el-menu-item v-for="(childrens, only) in children.children" :key="only"
              :index="children.path + '/' + childrens.path">
              <template #title>
                <svg-icon :icon-class="childrens.meta.icon" style="margin-right: 2px !important" />
                {{ childrens.meta.title }}
              </template>
            </el-menu-item>
          </el-sub-menu>
        </template>
      </el-sub-menu>
    </template>

    <!-- 顶部菜单超出数量折叠 -->
    <el-sub-menu :style="{ '--theme': theme }" index="more" v-if="topMenus.length > visibleNumber"
      style="margin-left: 15px">
      <template #title>更多菜单</template>
      <template v-for="(item, rowLength) in topMenus">
        <el-sub-menu :index="item.path" :key="item.path" style="margin-left: 15px" v-if="rowLength >= visibleNumber">
          <template #title>
            <svg-icon :icon-class="item.meta.icon" style="margin-right: 7px !important; color: #000" />
            <div style="color: #000">{{ item.meta.title }}</div>
          </template>

          <template v-for="(children, index) in item.children">
            <el-menu-item v-if="!children.children" :key="index" :index="children.path">
              <template #title>
                <svg-icon :icon-class="children.meta.icon" style="margin-right: 2px !important" />
                {{ children.meta.title }}
              </template>
            </el-menu-item>
          </template>

          <template v-for="(children, index) in item.children">
            <el-sub-menu :key="index" :index="children.path" v-if="children.children">
              <template #title>
                <svg-icon :icon-class="children.meta.icon" style="margin-right: 2px !important; color: #000" />
                <div style="color: #000">{{ children.meta.title }}</div>
              </template>

              <el-menu-item v-for="(childrens, only) in children.children" :key="only"
                :index="children.path + '/' + childrens.path">
                <template #title>
                  <svg-icon :icon-class="childrens.meta.icon" style="margin-right: 2px !important" />
                  {{ childrens.meta.title }}
                </template>
              </el-menu-item>
            </el-sub-menu>
          </template>
        </el-sub-menu>
      </template>
    </el-sub-menu>
  </el-menu>
</template>

<script setup>
import { constantRoutes } from "@/router";
import { isHttp } from "@/utils/validate";
import useAppStore from "@/store/modules/app";
import useSettingsStore from "@/store/modules/settings";
import usePermissionStore from "@/store/modules/permission";
import bus from "@/utils/eventBus";

// 顶部栏初始数
const visibleNumber = ref(null);
// 当前激活菜单的 index
const currentIndex = ref(null);
// 隐藏侧边栏路由
const hideList = ["/index", "/user/profile"];

const appStore = useAppStore();
const settingsStore = useSettingsStore();
const permissionStore = usePermissionStore();
const route = useRoute();
const router = useRouter();

// 主题颜色
const theme = computed(() => settingsStore.theme);
// 所有的路由信息
const routers = computed(() => permissionStore.topbarRouters);

// 顶部显示菜单
const topMenus = computed(() => {
  let topMenus = [];
  routers.value.map(menu => {
    if (menu.hidden !== true) {
      // 兼容顶部栏一级菜单内部跳转
      if (menu.path === "/") {
        topMenus.push(menu.children[0]);
      } else {
        topMenus.push(menu);
      }
    }
  });
  return topMenus;
});

// 设置子路由
const childrenMenus = computed(() => {
  let childrenMenus = [];
  routers.value.map(router => {
    for (let item in router.children) {
      if (router.children[item].parentPath === undefined) {
        if (router.path === "/") {
          router.children[item].path = "/" + router.children[item].path;
        } else {
          if (!isHttp(router.children[item].path)) {
            router.children[item].path =
              router.path + "/" + router.children[item].path;
          }
        }
        router.children[item].parentPath = router.path;
      }
      childrenMenus.push(router.children[item]);
    }
  });
  return constantRoutes.concat(childrenMenus);
});

// 默认激活的菜单
const activeMenu = computed(() => {
  const path = route.path;
  let activePath = path;
  if (
    path !== undefined &&
    path.lastIndexOf("/") > 0 &&
    hideList.indexOf(path) === -1
  ) {
    const tmpPath = path.substring(1, path.length);
    activePath = "/" + tmpPath.substring(0, tmpPath.indexOf("/"));
    appStore.toggleSideBarHide(false);
  } else if (!route.children) {
    activePath = path;
    appStore.toggleSideBarHide(true);
  }
  activeRoutes(activePath);
  return activePath;
});

function setVisibleNumber() {
  const width = document.body.getBoundingClientRect().width / 3;
  visibleNumber.value = parseInt(width / 100) + 1;
}

function handleSelect(key, keyPath) {
  currentIndex.value = key;
  const route = routers.value.find(item => item.path === key);
  if (isHttp(key)) {
    // http(s):// 路径新窗口打开
    window.open(key, "_blank");
  } else if (!route || !route.children) {
    // 没有子路由路径内部打开
    router.push({ path: key });
    appStore.toggleSideBarHide(true);
  } else {
    // 显示左侧联动菜单
    activeRoutes(key);
    appStore.toggleSideBarHide(false);
  }
}

function activeRoutes(key) {
  let routes = [];
  if (childrenMenus.value && childrenMenus.value.length > 0) {
    childrenMenus.value.map(item => {
      if (key == item.parentPath || (key == "index" && "" == item.path)) {
        routes.push(item);
      }
    });
  }
  if (routes.length > 0) {
    permissionStore.setSidebarRouters(routes);
  }
  return routes;
}

function handleIndex() {
  bus.emit('handleIndex')
}

onMounted(() => {
  setVisibleNumber();
  window.addEventListener("resize", setVisibleNumber);
});
onBeforeUnmount(() => {
  window.removeEventListener("resize", setVisibleNumber);
});

</script>

<style lang="scss">
#topmenu-container {
  // background-color: #1d694d;
  background-color: transparent;
  width: 100%;
}

#app .topmenu-container .is-active {
  border-bottom: 0px !important;
}

.topmenu-container.el-menu--horizontal>.el-menu-item {
  float: left;
  height: 35px !important;
  line-height: 35px !important;
  color: #999093 !important;
  padding: 0 5px !important;
  margin: 0 10px !important;
  border-radius: 10px 10px 0 0;
}

.topmenu-container.el-menu--horizontal>.el-menu-item.is-active,
.el-menu--horizontal>.el-sub-menu.is-active .el-submenu__title {
  border-bottom: 0 !important;
  color: #303133;
}

.el-menu--horizontal .el-menu .el-menu-item.is-active {
  color: var(--theme-deep-color) !important;
}

/* sub-menu item */
.topmenu-container.el-menu--horizontal>.el-sub-menu .el-sub-menu__title {
  float: left;
  height: 35px !important;
  line-height: 35px !important;
  color: #fff !important;
  padding: 0 5px !important;
  margin: 0 5px !important;
}

.topmenu-container.el-menu--horizontal>.el-sub-menu .el-sub-menu__title:hover {
  background-color: transparent;
}

// 首页按钮样式
// 首页背景色
.el-menu--horizontal>.el-menu-item.is-active {
  // background-color: #def3ea !important;
}

.el-sub-menu .el-sub-menu__icon-arrow {
  position: relative !important;
  right: -5px !important;
  top: 10% !important;
}
</style>
<style scoped>
.el-menu--horizontal>.el-sub-menu.is-active .el-sub-menu__title {
  border-bottom: 0px !important;
}
</style>
